import cloud from '@lafjs/cloud'
const db = cloud.mongo.db

export default async function (ctx: FunctionContext) {
  let data = ctx.body
  let moxueyuanMobiles = <any>[] //魔学院的涉及到的手机号

  let whereJson = {
    status: { $in: [2, 7, 10] }, // 有效订单
  }

  //未结束的订单 || 付款时间是传入年份的订单
  let withinScopeOrders = <any>[]

  //如果有时间筛选
  if (data.year) {
    //传入年份的start和end
    const startOfYear = new Date(data.year, 0, 1).getTime(); // 该年份的开始时间戳
    const endOfYear = new Date(data.year, 11, 31, 23, 59, 59, 999).getTime(); // 该年份的结束时间戳
    let bb = await db.collection("web-order").find({ _add_time: { $gte: startOfYear, $lt: endOfYear } }).project({ _add_time: 1 }).toArray()
    bb.forEach(element => {
      withinScopeOrders.push(element._id)
    });

    //有未结束的订单 || 传入时间内，支付结束的订单
    let filteredItems = <any>[{ status: 8 }]
    let newQuery = { balance_pay_info: { $exists: true } }
    const start_balance_pay = new Date(data.year, 0, 1).getTime(); // 该年份的开始时间戳
    const end_balance_pay = new Date(data.year, 11, 31, 23, 59, 59, 999).getTime(); // 该年份的结束时间戳
    newQuery["balance_pay_info.pay_time"] = { $gte: start_balance_pay, $lt: end_balance_pay }
    filteredItems.push(newQuery)
    let obj = { $or: filteredItems }  //最终的入参

    // 过滤掉不需要的字段
    let aa = await db.collection("web-order").find(obj).project({ _add_time: 1 }).toArray()
    aa.forEach(element => {
      withinScopeOrders.push(element._id)
    });
    whereJson["_id"] = { $in: withinScopeOrders } //特定的订单id
  }

  //课程筛选
  if (data.curriculum_id && data.curriculum_id.length) {
    whereJson["curriculum_id"] = { $in: data.curriculum_id }
  }

  //只展示魔学院里面的
  let moxueyuanAllUsers = await db.collection("moxueyuan-user-detail").find({}).project({ mobile: 1, disabled: 1 }).toArray()
  if (moxueyuanAllUsers.length) {
    moxueyuanAllUsers.forEach(element => {
      moxueyuanMobiles.push(element.mobile)
    });
    whereJson["mobile"] = { $in: moxueyuanMobiles }
  }

  let curriculums = await db.collection("web-course").find({}).project({ _id: 1, name: 1 }).toArray()

  let aa = await db.collection("web-order").find(whereJson).project({ mobile: 1, curriculum_id: 1 }).toArray()
  let result = cloud.utils.sameSortArr(aa, "curriculum_id")
  let maps = []
  result.forEach(element => {
    let index = cloud.utils.findIndexByField(curriculums, "_id", element.name)
    if (index != -1) {
      let query = {
        name: curriculums[index].name,
        curriculum_id: element.curriculum_id,
        curriculum_all: element.origin.length,
        curriculum_open: getOpen(element.origin, 0),
        curriculum_close: getOpen(element.origin, 1)
      }
      maps.push(query)
    }
  });

  function getOpen(array, key) {
    let arr = []
    array.forEach(element => {
      let index = cloud.utils.findIndexByField(moxueyuanAllUsers, "mobile", element.mobile)
      if (index != -1) {
        if (moxueyuanAllUsers[index].disabled == key) arr.push(element)
      }
    });
    return arr.length
  }
  return cloud.utils.requestReturn({ data: maps })
}
